/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Unity Technologies.
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

using System.Collections.Generic;
using System.IO;

namespace Microsoft.Unity.VisualStudio.Editor
{
	internal static class Discovery
	{
		public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
		{
			// 支持基于 VS Code 的 AI 编辑器
			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.Windsurf))
				yield return installation;

			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.Cursor))
				yield return installation;

			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.TraeCN))
				yield return installation;

			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.Lingma))
				yield return installation;

			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.Qoder))
				yield return installation;

			foreach (var installation in VSCodeBasedEditorInstallation.GetVisualStudioInstallations(VSCodeBasedEditorInstallation.EditorType.Kiro))
				yield return installation;
		}

		public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation)
		{
			try
			{
				// 尝试发现基于 VS Code 的 AI 编辑器
				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.Windsurf, out installation))
					return true;

				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.Cursor, out installation))
					return true;

				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.TraeCN, out installation))
					return true;

				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.Lingma, out installation))
					return true;

				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.Qoder, out installation))
					return true;

				if (VSCodeBasedEditorInstallation.TryDiscoverInstallation(editorPath, VSCodeBasedEditorInstallation.EditorType.Kiro, out installation))
					return true;
			}
			catch (IOException)
			{
				installation = null;
			}

			return false;
		}

		public static void Initialize()
		{
			VSCodeBasedEditorInstallation.Initialize();
		}
	}
}
